#!/usr/bin/env fish
set file $argv[1]
set w $argv[2]
set h $argv[3]
set x $argv[4]
set y $argv[5]

# argv1: file path
# argv2: height
function preview_with_bat
  set -l file $argv[1]
  set -l h $argv[2]
  if type -q bat
    bat --color=always -p -r :$h $file
  else
    cat $file
  end
end

set file_type (file -Lb --mime-type $file)
if string match -rq ^image $file_type; and type -q viu
  viu -s -b -w $w $file
  set -l last $status
  if test $last -eq 0
    exit 1
  end
end

function echo_file_type
  set_color -o blue # add bold and blue text
  printf 'File Type: '
  set_color white
  printf $file_type
  set_color normal
  printf '\n'
end

switch $file_type
  case application/zip
    if type -q unzip
      echo_file_type
      unzip -Z -1 $file # preview file list
      exit 1
    end
  case application/gzip
    echo $file_type
    tar -tf $file # preview file list
    exit 1
  case application/json
    preview_with_bat $file $h
    exit 1
  case inode/x-empty
    set_color -o blue # add bold and blue text
    printf 'Empty file\n'
    exit 1
  case '*'
end

if string match -rq ^text $file_type
  preview_with_bat $file $h
  exit 1
end

echo_file_type
